home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-09 | 9.6 KB | 270 lines | [TEXT/ToyS] |
- -- change this if you don't want to see every property every time
- property displayresult : true
-
- (* These lists contain the names of editors that might show up in a document to be
- * targeted by this script. If you're adding new part editors to the possible
- * set, include their names here if appropriate.
- *)
-
- property gScriptableParts : {"Sound Editor 1.0"}
-
- (* There's a bug in one sample part such that it returns null when
- asked to create an embedded frames iterator rather than throwing an
- error (and perhaps in OpenDoc since it doesn't check for null...) If
- you include this part or another with this bug in a document targeted
- by this script, be sure to list it here or this script will crash OpenDoc. *)
- property gKnownDangerousLeaves : {"Panel Editor 1.0"}
-
- on SimpleDisplay(str)
- display dialog str buttons {"OK"} default button "OK"
- end SimpleDisplay
-
- on ConstantToString(const)
- if const = bold then
- return "bold"
- else if const = plain then
- return "plain"
- else if const = italic then
- return "italic"
- else if const = underline then
- return "underline"
- else if const = outline then
- return "outline"
- else if const = shadow then
- return "shadow"
- else
- return "unknown constant"
- end if
- end ConstantToString
-
- on IfDisplay(result, comment, buttonText)
- if displayresult then
- set needsQuote to true
- if (class of result) = boolean then
- if result then
- set result to "true"
- else
- set result to "false"
- end if
- else if (class of result) = constant then
- set result to my ConstantToString(result)
- else if (class of result) ≠ string then
- set needsQuote to false
- end if
- set displayString to comment
- if needsQuote then
- set displayString to displayString & "\""
- end if
- set displayString to displayString & (result)
- if needsQuote then
- set displayString to displayString & "\""
- end if
- display dialog displayString buttons {buttonText} default button buttonText
- end if
- end IfDisplay
-
- (****************************************************************
- * Get and display every property accessible through the default
- * accessors/handlers. Some of these are read/write though we
- * don't demonstrate that here.
- ****************************************************************)
- on InfoForPart(partID)
- if partID ≠ 0 then
- tell application "Your Sample Document"
- tell part id partID
- my IfDisplay(name, "name of part is ", "Keen")
- -- sometimes "comment" doesn't work....
- my IfDisplay(comment, "comment of part is ", "Wow!")
- -- my IfDisplay(its category, "category of root part is ")
- my IfDisplay(part size, "part size of part is ", "Neato")
- my IfDisplay(creation date, "creation date of part is ", "Fabulous")
- my IfDisplay(modification date, "modification date of part is ", "Hot damn")
- my IfDisplay(author, "author of part is ", "Special")
- my IfDisplay(bundled, "bundled property of part is ", "Way cool, Dude")
- my IfDisplay(show links, "show links of part is ", "I'm so happy for you!")
- my IfDisplay(editor, "editor name of part is ", "Really?")
- my IfDisplay(id, "ID of part is ", "Do tell")
- my IfDisplay(stationery, "stationery property of part is ", "Na und?")
-
- end tell
- end tell
- end if
- end InfoForPart
-
- (****************************************************************
- * Starting with the ID passed in, build a flat list of that ID
- * and the IDs of all parts contained within that one. There's no
- * particular order to the IDs.
- * The TRY statement deals with parts that don't have any embedded
- * parts, which will often signal this by returning an error
- * rather than an empty list -- as do the Default Semantic
- * Interfaces's accessors.
- ****************************************************************)
- on BuildFullList(startID)
- tell application "Your Sample Document"
- set curlist to startID as list
- set finallist to {}
- set newlist to {1} -- will be discarded
- repeat until newlist = {}
- set newlist to {}
- repeat with oneID in curlist
- if editor of part id oneID is not in gKnownDangerousLeaves then
- try
- -- throw in a whose clause just to show that they work :-)
- set newlist to newlist & id of (every part whose name ≠ "improbable__name") of part id oneID
- on error
- end try
- end if
- end repeat
- set finallist to finallist & curlist
- set curlist to newlist
- end repeat
- end tell
- return finallist & curlist
- end BuildFullList
-
- (****************************************************************
- * create a list of editors in the document. Each entry is a list whose first element
- * is the name of the editor, and whose second is a list of IDs of parts bound to
- * the named editor.
- ****************************************************************)
- on RecordInfoOnPart(edtr, partID, partInfoList)
- set tmplist to contents of partInfoList
- repeat with entry in tmplist
- if edtr = item 1 of entry then
- set sublist to item 2 of entry
- set item 2 of entry to sublist & {partID}
- return
- end if
- end repeat
- -- if we get here there's no entry
- set newlist to {edtr, {partID}}
- set contents of partInfoList to tmplist & {newlist}
- end RecordInfoOnPart
-
- (****************************************************************
- * Go through the editors record, displaying each editor's name
- * and the number of parts it has in the document.
- ****************************************************************)
- on DisplayPartInfoRecord(infoList)
- set str to "Editors represented and by how many parts:" & return
- repeat with entry in infoList
- set numEditors to length of item 2 of entry
- set str to str & item 1 of entry & ": " & (numEditors as text) & ";" & return
- end repeat
- my SimpleDisplay(str)
- end DisplayPartInfoRecord
-
- (****************************************************************
- * GetCurrentDate: for some reason, Sound Editor eats the event
- * intended for the current date osax, so we have to move it outside
- * its scope.
- ****************************************************************)
- on GetCurrentDate()
- return current date
- end GetCurrentDate
-
- (****************************************************************
- * For each editor, try to make it play (with the expectation that only
- * Sound Editor will do so). The TRY statement here assumes that if
- * one part fails it's because the editor doesn't understand playing
- * -- so we don't bother trying to play any remaining parts
- * with that editor.
- ****************************************************************)
- on TryToPlay(partInfoList)
- tell application "Your Sample Document"
- repeat with partrecord in partInfoList
- repeat with partID in item 2 of partrecord
- try
- tell part id partID
- play
- set soundLength to sound length
- set stopTime to (my GetCurrentDate()) + soundLength
- repeat until my GetCurrentDate() ≥ stopTime
- end repeat
- stop
- end tell
- on error
- my SimpleDisplay("Editor \"" & editor of part id partID & "\" won't play with me.")
- set comment of part id partID to "I'm not a player."
- exit repeat -- we know the part can't handle it now, so bail on the rest of the list
- end try
- end repeat
- end repeat
- end tell
- end TryToPlay
-
- (****************************************************************
- * Given the editors record, return the ID of a part with a given
- * editor. Right now we just return the first one.
- ****************************************************************)
- on GetPartThatMatches(partNameList, partInfoList, matchSought)
- repeat with partrecord in partInfoList
- if matchSought and item 1 of partrecord is in partNameList then
- return item 1 of item 2 of partrecord
- else if not matchSought and item 1 of partrecord is not in partNameList then
- return item 1 of item 2 of partrecord
- end if
- end repeat
- return 0
- end GetPartThatMatches
-
- (****************************************************************
- * Given a list of IDs of parts in the document, find the one
- * most deeply nested and return. We could do this without
- * the container property, but the whole point is to show it off.
- ****************************************************************)
- on FindMaxDepth(partIDList)
- set maxSoFar to 0
- tell application "Your Sample Document"
- set rootID to id
- repeat with oneID in partIDList
- set thisMax to 0
- set thisParent to contents of oneID
- repeat until thisParent = rootID
- set thisParent to id of (get container of part id thisParent)
- set thisMax to thisMax + 1
- end repeat
- if thisMax > maxSoFar then set maxSoFar to thisMax
- end repeat
- end tell
- return maxSoFar
- end FindMaxDepth
-
- -- HERE IS WHERE "MAIN" BEGINS....
-
- tell application "Your Sample Document"
- set comments to "I'm the root part"
-
- set listOfAllPartIDs to my BuildFullList(id)
-
- set partInfoList to {}
- repeat with partID in listOfAllPartIDs
- set edtr to editor of part id partID
- my RecordInfoOnPart(edtr, contents of partID, a reference to partInfoList)
- end repeat
-
- set aPartID to my GetPartThatMatches(gScriptableParts, partInfoList, true)
- if aPartID ≠ 0 then
- my SimpleDisplay("Now we'll show the info properties for a scriptable part")
- my InfoForPart(aPartID)
- else
- my SimpleDisplay("Couldn't find a scriptable part: did you include one we don't know about?")
- end if
-
- set aPartID to my GetPartThatMatches(gScriptableParts, partInfoList, false)
- if aPartID ≠ 0 then
- my SimpleDisplay("Now we'll show the info properties for a non-scriptable part")
- my InfoForPart(aPartID)
- else
- my SimpleDisplay("Couldn't find a non-scriptable part: did you include one we don't know about?")
- end if
-
- my DisplayPartInfoRecord(partInfoList)
- my SimpleDisplay("now I'll play all the parts that'll let me")
- my TryToPlay(partInfoList)
-
- my SimpleDisplay("The greatest depth of nesting in this document is " & my FindMaxDepth(listOfAllPartIDs) & ".")
-
- my SimpleDisplay("END OF DEMO")
- end tell